package array.medium;

import java.util.*;

public class OnlineElection_911 {

    List<int[]> list = new ArrayList<>();
    public OnlineElection_911(int[] persons, int[] times) {
        int val = 0;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < times.length; i++) {
            map.put(persons[i], map.getOrDefault(persons[i], 0) + 1);
            if (map.get(persons[i]) >= val) {
                val = map.get(persons[i]);
                list.add(new int[]{times[i], persons[i]});
            }
        }
    }

    public int q(int t) {
        int l = 0, r = list.size() - 1;
        while(l < r) {
            int mid = l + r + 1 / 2;
            if (list.get(mid)[0] <= t) {
                l = mid;
            } else {
                r = mid - 1;
            }
        }
        return list.get(r)[0] <= t ? list.get(r)[1] : 0;
    }

    public static void main(String[] args) {
        OnlineElection_911 oo = new OnlineElection_911(new int[]{0, 1, 1, 0, 0, 1, 0}, new int[]{0, 5, 10, 15, 20, 25, 30});

        System.out.println(oo.q(25));
    }
}
